package com.example.demo.dao;

import com.example.demo.entity.Student;
import com.example.demo.entity.Stus;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.util.List;

/**
 * JPA的数据库操作类
 */
//第一个参数你要操作的类名,第二个参数这个实体类的主键  类型
public interface StuJpaDao extends JpaRepository<Student,Integer> {
    //查找学号为no的学生信息
    @Query(value = "select * from stu where sex = ?2 and  id=?1",nativeQuery = true)
    Student getStu(int id,String sex) ;

    //更新学生姓名
    @Transactional
    @Modifying
    @Query(value = "update stu set name=?2  where id=?1",nativeQuery = true)
    int updateStu(int id, String newName);


    //删除指定ID的学生
    @Transactional
    @Modifying
    @Query(value = "delete from stu where id=?1",nativeQuery = true)
    public int deleteSql(int id);

//    等价于select * from stu where name like %name%
    List<Student> findByNameContaining(String name);

    //    等价于select * from stu where name like '%name%' and classId = classId
    List<Student> findByNameContainingAndClassId(String name,int classId);

    //等价于select * from stu where name like '%蓝%' and age=11
    List<Student> findByNameContainingAndAge(String name,int age);
}
